package com.zmn.uac.convert;

import com.alibaba.nacos.api.config.convert.NacosConfigConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.boot.logging.LogLevel;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

/**
 * @title: 动态日志级别转换器
 * @author:Rananquan
 * @Date:2022/1/13 10:04
 */
@Slf4j
public class LoggingLevelNacosConvert implements NacosConfigConverter<HashMap> {
    @Override
    public boolean canConvert(Class<HashMap> targetType) {
        return Boolean.TRUE;
    }

    @Override
    public HashMap convert(String config) {
        HashMap<String, LogLevel> map = new HashMap<>(1);
        Properties properties = new Properties();
        try {
            properties.load(new ByteArrayInputStream(config.getBytes()));
            map.put("zmn.logging.level",LogLevel.valueOf(properties.getProperty("zmn.logging.level", "INFO")));
        } catch (IOException e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
        }

        return map;
    }
}
